# Dynamic entity class
from math import sin,cos
from pyston import *
from Entity import *

class DynamicEntity(Entity):
    # Constructor
    def __init__(self, primarySurface = None):
        Entity.__init__(self)
        
        # Naming
        self.type = "Dynamic"
        self.state = "default"
        
        # Physics
        self.x = 0
        self.y = 0
        self.w = 0
        self.h = 0
        self.vx = 0
        self.vy = 0
        self.mass = 1
        
        # Drawing
        self.displayMode = dm_static
        self.image = None
    
    # Core methods
    # Step frame
    def stepFrame(self, timeSinceLastFrame):
        # Call the entity stepFrame to update the tick logic
        Entity.stepFrame(self, timeSinceLastFrame)

    # Drawing methods
    def draw(self, primarySurface):
        # Kill the function if image and blitsurface are None
        if self.image == None or primarySurface == None:
            return
        
        primarySurface.blit(self.image.frame, (self.x, self.y))
    
    # Set image takes a returned resource and loads it
    def setImage(self, resource):
        self.image = resource
        
        # Finally set dimensions
        self.setDimensions(self.image.rect[2], self.image.rect[3])
    
    # Load image invokes the resource manager and gets a named resource
    # If no resource manager is assigned, one can be given as a parameter with the option to replace the internal one    
    def loadImage(self, resourceName, newResourceManager = None, replaceCurrent = False):
        # If the resource manager isnt set, try to use the pointer
        if self.resourceManager == None:
            if newResourceManager != None:
                self.image = newResourceManager.getImage(resourceName)
                
                # Are we also replacing the resource manager?
                if replaceCurrent == True:
                    self.resourceManager = self.newResourceManager
            else:
                debug_out("DynamicEntity - loadImage(): No valid resource manager set.")
        else:
            self.image = self.resourceManager.getImage(resourceName)
            
        # Finally set dimensions
        self.setDimensions(self.image.rect[2], self.image.rect[3])
    
    # Loads the image file directly either through a resource manager or a temporary resource    
    def loadImageFile(self, fileName):
        # If the resource manager isnt set, create a temporary version and load from that
        if self.resourceManager == None:
            tmpResourceManager = ResourceManager()
            tmpResourceManager.loadImage("temp", fileName)
            self.image = tmpResourceManager.getImage("temp")
            debug_out("DynamicEntity - loadImageFile(): Warning - No valid resource manager set, created temporary copy.")
        else:
            self.image = self.resourceManager.loadResource(None, fileName)
            
        # Finally set dimensions
        self.setDimensions(self.image.rect[2], self.image.rect[3])

    # State management
    #
    # Set - Overrides active state (doesnt affect stack)
    # Get - Returns current state
    def setState(self, newState = "default"):
        self.state = newState
    
    def getState(self):
        return self.state
    
    # Movement methods
    # Set position
    def setPosition(self, toX = 0, toY = 0):
        self.x = toX
        self.y = toY
    
    # Dimensions    
    def setDimensions(self, newW, newH):
        self.w = newW
        self.h = newH
    
    # Application of force - takes force amount (pixels per frame) and direction (degrees, 0 = up)
    def applyForce(self, forceAmount = 0, direction = 0):
        self.vx += forceAmount * cos(direction)
        self.vy += forceAmount * sin(direction)